package com.hbyj.struts.action.authority;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;


import javax.management.relation.RoleList;
import javax.servlet.http.HttpServletRequest;


import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;

import com.hbyj.db.function.DbTools;
import com.hbyj.hbm.vo.Functions;
import com.hbyj.hbm.vo.MemberRole;
import com.hbyj.hbm.vo.Members;
import com.hbyj.hbm.vo.MrBaseInf;
import com.hbyj.hbm.vo.RoleFunction;
import com.hbyj.hbm.vo.Roles;
import com.hbyj.object.AuthorityFunctionData;
import com.hbyj.object.AuthorityFunctionDataSet;
import com.hbyj.object.FlexiGridData;
import com.hbyj.object.IFlexiGridable;
import com.hbyj.object.RoleAddData;
import com.hbyj.object.RoleForFlexiGrid;
import com.hbyj.object.RoleFunctionForZTree;
import com.hbyj.object.RoleWithFunction;
import com.hbyj.object.ZTreeNode;
import com.opensymphony.xwork2.ActionSupport;
import com.sun.xml.internal.bind.v2.runtime.unmarshaller.XsiNilLoader.Array;

@SuppressWarnings("serial")
public class AuthorityRoleAction extends ActionSupport {
	private String jsonResult;
	
	public String getJsonResult() {
		return jsonResult;
	}

	public void setJsonResult(String jsonResult) {
		this.jsonResult = jsonResult;
	}
	
	public void role_add() throws UnsupportedEncodingException, IOException{
		Boolean rlt = true;
		try {
			HttpServletRequest request = ServletActionContext.getRequest();

			//List departCategoryList = DbTools.findByProperty("DepartmentCategory", "name", category);
		    String postString = request.getParameter("ruleObj");
		    JSONObject jsonObj = JSONObject.fromObject(postString);
		    RoleAddData  roleArray= ( RoleAddData)JSONObject.toBean(jsonObj, RoleAddData.class);
		    roleArray.saveRoleRelation();

		    ServletActionContext.getResponse().getOutputStream().write(rlt.toString().getBytes("utf-8"));
		} catch (Exception e) {
			e.printStackTrace();
			ServletActionContext.getResponse().getOutputStream().write(e.toString().getBytes("utf-8"));
		}
	}
	
	public void role_update() throws UnsupportedEncodingException, IOException{
		Boolean rlt = true;
		try {
			HttpServletRequest request = ServletActionContext.getRequest();

			//List departCategoryList = DbTools.findByProperty("DepartmentCategory", "name", category);
		    String postString = request.getParameter("ruleObj");
		    JSONObject jsonObj = JSONObject.fromObject(postString);
		    RoleAddData  roleArray= ( RoleAddData)JSONObject.toBean(jsonObj, RoleAddData.class);
		    roleArray.updateRoleRelation();
 
		    ServletActionContext.getResponse().getOutputStream().write(rlt.toString().getBytes("utf-8"));
		} catch (Exception e) {
			e.printStackTrace();
			ServletActionContext.getResponse().getOutputStream().write(e.toString().getBytes("utf-8"));
		}
	}
	
	public void role_delete() throws UnsupportedEncodingException, IOException{
		try {
			HttpServletRequest request = ServletActionContext.getRequest();
			Integer role_id = Integer.parseInt(request.getParameter("role_id"));
			String hqlString="delete from Roles role where role.id="+role_id;
			Boolean rltBoolean=DbTools.delete(hqlString);
			ServletActionContext.getResponse().getOutputStream().write((rltBoolean.toString()).getBytes("utf-8"));
		} catch (Exception e) {
			e.printStackTrace();
			ServletActionContext.getResponse().getOutputStream().write((e.toString()).getBytes("utf-8"));
		}
	}
	
	public void authority_query() throws UnsupportedEncodingException, IOException{
		HttpServletRequest request = ServletActionContext.getRequest();
		String mmenuID = request.getParameter("mmid");
		Members mbInfo = (Members)request.getSession().getAttribute("SysUser");
		String userName = mbInfo.getName();
		List mLists=DbTools.findByHql("from Members m where m.name='"+userName+"'");
		Members member = (Members)mLists.get(0);
		List memberRoleList = DbTools.findByHql("from MemberRole mr where mr.memberId="+member.getId());
		MemberRole memberRole = (MemberRole)memberRoleList.get(0);
		Roles roles = memberRole.getRoles();
		Set roleFunctionSet = (Set)roles.getRoleFunctions();
		List<AuthorityFunctionData> roleFunctionList  = new ArrayList<AuthorityFunctionData>();
		for(Object obj:roleFunctionSet){
			RoleFunction rolefunction = (RoleFunction)obj;
			Functions function = rolefunction.getFunctions();
			int parentId=function.getParentId();
			Boolean isCurrentMID = (parentId==Integer.parseInt(mmenuID))?true:false;
		    Boolean canread = rolefunction.getR();
		    if(isCurrentMID&&canread){
		    	AuthorityFunctionData data = new AuthorityFunctionData();
		    	data.setFunctionName(function.getName());
		    	data.setC(rolefunction.getC());
		    	data.setU(rolefunction.getU());
		    	data.setR(rolefunction.getR());
		    	data.setD(rolefunction.getD());
		    	roleFunctionList.add(data);
		    }
		}
		AuthorityFunctionDataSet set = new AuthorityFunctionDataSet();
		set.setData(roleFunctionList);
		String rltString = JSONObject.fromObject(set).toString();
		ServletActionContext.getResponse().getOutputStream().write(rltString.getBytes("utf-8"));
	}
	
	
	public void roleFunction_query() throws UnsupportedEncodingException, IOException{
		try {
			HttpServletRequest request = ServletActionContext.getRequest();
			Integer id = Integer.parseInt(request.getParameter("role_id"));
			
			List functionsByHiberObjList = DbTools.findByHql("from Functions");
			String hqlString = "from RoleFunction rolwfunction where rolwfunction.roles.id="+id;
			List roleFunctionByHiberObjList = DbTools.findByHql(hqlString);
			List<ZTreeNode> listFunctionStruct = new ArrayList<ZTreeNode>();
			List<RoleWithFunction> listRoleFunctions = new ArrayList<RoleWithFunction>();
			
			for (Object nodeObj : functionsByHiberObjList) {
				Functions node = (Functions)nodeObj;
				ZTreeNode node2 = new ZTreeNode(node);
				listFunctionStruct.add(node2);
			}
			
			for (Object temp : roleFunctionByHiberObjList) {
				RoleFunction hiberObjFunction =(RoleFunction)temp;
				RoleWithFunction roleFunction = new RoleWithFunction(hiberObjFunction);
				listRoleFunctions.add(roleFunction);
			}
			RoleFunctionForZTree rltObj = new RoleFunctionForZTree(listFunctionStruct,listRoleFunctions);
			String rltString = JSONObject.fromObject(rltObj).toString();
			ServletActionContext.getResponse().getOutputStream().write(rltString.getBytes("utf-8"));
			
		} catch (Exception e) {
			 e.printStackTrace();
			ServletActionContext.getResponse().getOutputStream().write(e.toString().getBytes("utf-8"));
		}
	}
	
	public void role_query() throws UnsupportedEncodingException, IOException{
		try {
			HttpServletRequest request = ServletActionContext.getRequest();

			//List departCategoryList = DbTools.findByProperty("DepartmentCategory", "name", category);
		    String rolename = request.getParameter("rolename");
		    Integer page = Integer.parseInt(request.getParameter("page"));
		    String hqlString = "from Roles";
		    if(rolename!="")
		    	hqlString+=" roles where roles.name='"+rolename+"'";
		    List rolesList = DbTools.findByHql(hqlString);
		    int roleCount = rolesList.size();
		    if (roleCount==0) {
				return;
			}
		    List<IFlexiGridable> flexiGridables = new ArrayList<IFlexiGridable>();
		    for(int i=0;i<roleCount;i++){
		    	IFlexiGridable roleForFlexiGrid = new RoleForFlexiGrid();
		    	flexiGridables.add(roleForFlexiGrid);
		    }
		    
		    FlexiGridData<RoleForFlexiGrid> datas = new 
		    	FlexiGridData<RoleForFlexiGrid>(roleCount, page, rolesList, flexiGridables);
		      String  jsonText= JSONObject.fromObject(datas).toString();
			  ServletActionContext.getResponse().getOutputStream().write(jsonText.getBytes("utf-8"));
		} catch (Exception e) {
		    e.printStackTrace();
			ServletActionContext.getResponse().getOutputStream().write(e.toString().getBytes("utf-8"));
		}
	}
}

